package com.lixl.collection;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

public class ListDemo {
	
	public static void main(String[] args) {
		//linkedListDemo();
		twoListMerge();
	}
	
	public static void linkedListDemo(){
		LinkedList<String> list = new LinkedList<String>();
		list.add("a1");
		list.add("a2");
		list.add("a3");
		list.addFirst("a4");
		list.addLast("a5");
		System.out.print("遍历LinkedList ");
		//list.remove()	//
		for(String s:list){
			System.out.print(s+",");
		}
		System.out.println();
		System.out.println("peek = "+list.peek());
		System.out.println(list.element());
	}
	
	public static void twoListMerge(){
		List<String> listA = new ArrayList<String>();
		listA.add("a1");
		listA.add("a2");
		listA.add("a3");
		listA.add("a4");
		listA.add("a5");
		listA.add("a6");
		listA.add("a7");
		System.out.println("listA = "+listA);
		
		List<String> listB = new ArrayList<String>();
		listB.add("b1");
		listB.add("a1");
		listB.add("b2");
		listB.add("a3");
		System.out.println("listB = "+listB);
		
		listA.addAll(listB);
		System.out.println("listA.addAll(listB) "+listA);
		
		listA.removeAll(listB);
		System.out.println("listA.removeAll(listB) "+listA);
		
		listA.addAll(listB);
		System.out.println("listA.addAll(listB) "+listA);
	}
	
	public static void twoListMerge(List<String> listA, List<String> listB){
		
	}
	
	public static void arrayListDemo(){
		ArrayList<String> list = new ArrayList<String>();
		System.out.println(list.size());
		
		list.add("a");
		list.add("b");
		list.add("a");
		list.add("b");
		
		//list->[a, b, a, b]
		System.out.println(list.size());
		
		list.remove("a");//只删除第一个相同的元素
		//list->[b, a, b]
		System.out.println(list.size());
		
		//可以使用for循环遍历；
		//但遍历的过程中不能使用remove方法，否则会报java.util.ConcurrentModificationException异常
		int i=0;
		for(String s:list){
			if(s.equals("b")){
				//list.remove(s);
				list.set(i, "c");
			}
			i++;
		}
		
		//list->[c, a, c]
		
		//正确使用Iterator遍历并remove数据
		Iterator<String> it = list.iterator();
		while(it.hasNext()){
			String s = it.next();
			if("a".equals(s)){
				it.remove();
			}
		}
		System.out.println(list.size());
		//Arrays.
	}

}
